<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <input type="text" class="ipt" />
    <script>
      //在输入框输入内容后,等待1s后发送请求,如果1s内输入框内容改变了,则重新等待1s(防抖)

      function debounce(fn, delay) {
        //fn 是需要执行的函数逻辑
        //delay 间隔的时间
        var timer;
        return function () {
          clearTimeout(timer);
          if (!timer) {
            fn();
          }
          timer = setTimeout(function () {
            timer = null;
          }, delay);
        };
      }

      const ipt = document.querySelector(".ipt");
      ipt.onkeyup = debounce(function () {
        console.log(ipt.value);
      }, 1000);
    </script>
  </body>
</html>
